package com.ihisun.tmhcms.core.service.giftbagservice.impl;

import com.ihisun.tmhcms.core.dao.citydao.CityDao;
import com.ihisun.tmhcms.core.dao.edgiftbagdao.EditVipPackageMechanismDao;
import com.ihisun.tmhcms.core.dao.edgiftbagdao.EditgiftbagDao;
import com.ihisun.tmhcms.core.dao.edgiftbagdao.VipOnTheShelfCityDao;
import com.ihisun.tmhcms.core.dao.edgiftbagdao.VipOnTheShelfCityVoDao;
import com.ihisun.tmhcms.core.dao.mechanismdao.MechanismDao;
import com.ihisun.tmhcms.core.dto.citydto.CityDto;
import com.ihisun.tmhcms.core.dto.editgiftbag.EditVipPackageDto;
import com.ihisun.tmhcms.core.dto.editgiftbag.SearchGiftBagDto;
import com.ihisun.tmhcms.core.dto.editgiftbag.VipOnTheShelfCityDto;
import com.ihisun.tmhcms.core.dto.editgiftbag.VipPackageMechanismDto;
import com.ihisun.tmhcms.core.dto.mechanismdto.EditMechanismDto;
import com.ihisun.tmhcms.core.service.giftbagservice.GiftBagService;
import com.ihisun.tmhcms.core.vo.MechanismVo;
import com.ihisun.tmhcms.core.vo.VipOnTheShelfCityVo;
import com.ihisun.tmhcms.core.vo.VipPackageVo;
import com.ihisun.tmhcms.utils.DateUtils;
import com.ihisun.tmhcms.utils.PageBean;
import com.ihisun.tmhcms.utils.ResponseVo;
import com.ihisun.tmhcms.utils.UuIdUtil;
import org.springframework.security.core.parameters.P;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import java.math.BigInteger;
import java.util.List;

@Service
public class GiftBagServiceImpl implements GiftBagService {
    @Resource
    EditgiftbagDao editgiftbagDao;
    @Resource
    VipOnTheShelfCityDao vipOnTheShelfCityDao;
    @Resource
    EditVipPackageMechanismDao editVipPackageMechanismDao;
    @Resource
    CityDao cityDao;
    @PersistenceContext
    EntityManager entityManager;
    @Resource
    MechanismDao mechanismDao;
    @Resource
    VipOnTheShelfCityVoDao vipOnTheShelfCityVoDao;

    @Transactional
    @Override
    public ResponseVo editGiftBag(EditVipPackageDto editVipPackageDto) {

        if (editVipPackageDto.getPackageId() != null && !"".equals(editVipPackageDto.getPackageId())) {
            /**
             * 修改礼包基本信息
             */
            editVipPackageDto.setUpdateTime(DateUtils.getCurrentTime());
            editgiftbagDao.save(editVipPackageDto);


            List<VipOnTheShelfCityDto> vipOnTheShelfCityDtos = editVipPackageDto.getVipOnTheShelfCityDtos();
            if (vipOnTheShelfCityDtos != null && !vipOnTheShelfCityDtos.isEmpty()) {
                /**
                 * 删除礼包上架城市
                 */
                vipOnTheShelfCityDao.deleteByPackageId(editVipPackageDto.getPackageId());
                /**
                 * 保存礼包上架城市
                 */

                for (int i = 0; i < vipOnTheShelfCityDtos.size(); i++) {
                    vipOnTheShelfCityDtos.get(i).setCreateTime(DateUtils.getCurrentTime());
                    vipOnTheShelfCityDtos.get(i).setUpdateTime(DateUtils.getCurrentTime());
                    vipOnTheShelfCityDtos.get(i).setPackageId(editVipPackageDto.getPackageId());
                    vipOnTheShelfCityDao.save(vipOnTheShelfCityDtos.get(i));
                }
            }


            List<VipPackageMechanismDto> vipPackageMechanismDtos = editVipPackageDto.getVipPackageMechanismDtos();
            if (vipPackageMechanismDtos != null && !vipPackageMechanismDtos.isEmpty()) {
                /**
                 * 删除绑定的机构
                 */
                editVipPackageMechanismDao.deleteByPackageId(editVipPackageDto.getPackageId());

                /**
                 * 保存绑定的机构
                 */

                for (int i = 0; i < vipPackageMechanismDtos.size(); i++) {
                    vipPackageMechanismDtos.get(i).setCreateTime(DateUtils.getCurrentTime());
                    vipPackageMechanismDtos.get(i).setUpdateTime(DateUtils.getCurrentTime());
                    vipPackageMechanismDtos.get(i).setPackageId(editVipPackageDto.getPackageId());
                    editVipPackageMechanismDao.save(vipPackageMechanismDtos.get(i));
                }
            }
        } else {
            /**
             * 保存基本信息
             */
            String uuid = UuIdUtil.getToUuid();
            editVipPackageDto.setPackageId(uuid);
            editVipPackageDto.setUpdateTime(DateUtils.getCurrentTime());
            editVipPackageDto.setCreateTime(DateUtils.getCurrentTime());
            editgiftbagDao.save(editVipPackageDto);
            /**
             *保存上架城市
             */
            List<VipOnTheShelfCityDto> vipOnTheShelfCityDtos = editVipPackageDto.getVipOnTheShelfCityDtos();
            if (vipOnTheShelfCityDtos != null && !vipOnTheShelfCityDtos.isEmpty()) {
                for (int i = 0; i < vipOnTheShelfCityDtos.size(); i++) {
                    vipOnTheShelfCityDtos.get(i).setPackageId(uuid);
                    vipOnTheShelfCityDtos.get(i).setUpdateTime(DateUtils.getCurrentTime());
                    vipOnTheShelfCityDtos.get(i).setCreateTime(DateUtils.getCurrentTime());
                    vipOnTheShelfCityDao.save(vipOnTheShelfCityDtos.get(i));
                }

            }
            List<VipPackageMechanismDto> vipPackageMechanismDtos = editVipPackageDto.getVipPackageMechanismDtos();
            if (vipPackageMechanismDtos != null && !vipPackageMechanismDtos.isEmpty()) {
                for (int i = 0; i < vipPackageMechanismDtos.size(); i++) {
                    vipPackageMechanismDtos.get(i).setPackageId(uuid);
                    vipPackageMechanismDtos.get(i).setUpdateTime(DateUtils.getCurrentTime());
                    vipPackageMechanismDtos.get(i).setCreateTime(DateUtils.getCurrentTime());
                    editVipPackageMechanismDao.save(vipPackageMechanismDtos.get(i));
                }
            }


        }


        return null;
    }

    @Override
    public ResponseVo searchGiftBag(SearchGiftBagDto searchGiftBagDto) {
        StringBuilder sql = new StringBuilder("select * from v_vip_package where state!=3");
        if (searchGiftBagDto.getPackageName() != null && !"".equals(searchGiftBagDto.getPackageName())) {
            sql.append(" and package_name like concat('%'," + searchGiftBagDto.getPackageName() + ",'%') ");
        }
        if (searchGiftBagDto.getState() != null && !"".equals(searchGiftBagDto.getState())) {
            sql.append(" and state=" + searchGiftBagDto.getState() + "");
        }
        sql.append(" limit  " + (searchGiftBagDto.getPageNum() - 1) * 10 + ", " + searchGiftBagDto.getPageSize() + "");
        List<VipPackageVo> resultList = entityManager.createNativeQuery(sql.toString(), VipPackageVo.class).getResultList();
        StringBuilder countSql = new StringBuilder("select count(*) from v_vip_package where state!=3");
        if (searchGiftBagDto.getPackageName() != null && !"".equals(searchGiftBagDto.getPackageName())) {
            sql.append(" and package_name like concat('%'," + searchGiftBagDto.getPackageName() + ",'%') ");
        }
        if (searchGiftBagDto.getState() != null && !"".equals(searchGiftBagDto.getState())) {
            sql.append(" and state=" + searchGiftBagDto.getState() + "");
        }
        BigInteger singleResult = (BigInteger) entityManager.createNativeQuery(countSql.toString()).getSingleResult();
        PageBean pageBean = new PageBean(searchGiftBagDto.getPageNum(), singleResult.intValue(), searchGiftBagDto.getPageSize(), resultList);
        return ResponseVo.ok(pageBean);
    }

    @Override
    public ResponseVo updateState(String packageId, String state) {
        EditVipPackageDto byPackageId = editgiftbagDao.findByPackageId(packageId);
        byPackageId.setState(state);
        editgiftbagDao.save(byPackageId);
        return ResponseVo.ok();
    }

    @Override
    public ResponseVo packageInfo(String packageId) {
        EditVipPackageDto byPackageId = editgiftbagDao.findByPackageId(packageId);
        List<VipOnTheShelfCityDto> byPackageId1 = vipOnTheShelfCityDao.findByPackageId(packageId);
        if (byPackageId1 != null && !byPackageId1.isEmpty()) {
            for (int i = 0; i < byPackageId1.size(); i++) {
                CityDto byCityCode = cityDao.findByCityCode(byPackageId1.get(i).getCity());
                byPackageId1.get(i).setCityName(byCityCode.getName());
            }
        }

        byPackageId.setVipOnTheShelfCityDtos(byPackageId1);
        return ResponseVo.ok(byPackageId);
    }

    @Override
    public ResponseVo findByMechanism() {
        List<MechanismVo> byAll = mechanismDao.findByAll();
        return ResponseVo.ok(byAll);
    }

    @Override
    public ResponseVo findByPackageMechanisnm(String packageId) {
        List<VipPackageMechanismDto> byPackageId = editVipPackageMechanismDao.findByPackageId(packageId);
        return ResponseVo.ok(byPackageId);
    }

    @Override
    public ResponseVo findByPackageCity(String packageId) {
        List<VipOnTheShelfCityVo> byPackageId = vipOnTheShelfCityVoDao.findByPackageId(packageId);
        if (byPackageId != null && !byPackageId.isEmpty()) {
            for (int i = 0; i < byPackageId.size(); i++) {
                CityDto byCityCode = cityDao.findByCityCode(byPackageId.get(i).getCity());
                byPackageId.get(i).setCityName(byCityCode.getName());
            }

        }
        return ResponseVo.ok(byPackageId);
    }
}
